# -*- coding: utf-8 -*-
'''
Created on 2 sept. 2012

@author: Olivier
'''
import socket               # Import socket module
import threading            # Import threading module
from ServerClientTreatment import ServerClientTreatment

class Server(threading.Thread):
    '''
    Class contains the server 
    This class accept the new connection, and create a thread to treat this new connection with a client 
    '''
    
    #Global constant
    NUMBER_PLAYER_MAX = 2
    
    def __init__(self, inIp, inPort, inController):
        '''
        Init the server
        @inIp: the Ip of the server
        @inPort: the port of the server
        '''
        threading.Thread.__init__(self)
        self.__mPlayerList = []
        
        self.__mIp   = inIp #ip of the server
        self.__mPort = inPort #port of the server
        self.__mController = inController
        
        self.__mStopServer = False
        self.__mClientConnectedList = []
        self.__mSocket = socket.socket()         # Create a socket object
        self.__mSocket.bind((self.__mIp, self.__mPort))        # Bind to the port
        self.__mBitePoint = (0,0)
        self.__mClickPoint = (-100, -100)
           
    def GetBitePoint(self):
        return self.__mBitePoint
    
    def GetClickPoint(self):
        return self.__mClickPoint
    
    def SetBitePoint(self, inPoint):
        self.__mBitePoint = inPoint
    
    def SetClickPoint(self, inPoint):
        self.__mClickPoint = inPoint
         
    def run(self):
        '''
        Function executed by the thread
        '''
        print "########\nServer Start\n########"
        self.__mStopServer = False               #Init the bool before start thread function
        self.__mSocket.listen(self.NUMBER_PLAYER_MAX)                 # Now wait for client connection.
        while not self.__mStopServer:
            c, addr =  self.__mSocket.accept()     # Establish connection with client.
            theNewSCT = ServerClientTreatment(c, addr, self)
            self.__mClientConnectedList.append(theNewSCT)
            self.SendToAllExcept('New Client Connected', theNewSCT)
    
    def RemoveServerClientTreatment(self, inServerClientTreatment):
        '''
        Remove a ServerClientTreatment from the list (use after disconnection)
        '''
        self.__mClientConnectedList.remove(inServerClientTreatment)
      
    def SendToAll(self, inMessage):
        '''
        function to send a message to all players
        '''
        for theSCT in self.__mClientConnectedList:
            theSCT.Send(inMessage)
            
    def SendToAllExcept(self, inMessage, inServerClientTreatmentExclude):
        '''
        function to send a message to all players except one
        '''
        for theSCT in self.__mClientConnectedList:
            if theSCT == inServerClientTreatmentExclude: continue
            theSCT.Send(inMessage)
            
    def stop(self):
        '''
        Stop the server
        '''
        self.__mStopServer = True
            
    def GetController(self):
        '''
        return the controller
        '''
        return self.__mController
    
    def GetPlayerList(self):
        return self.__mPlayerList
    
if __name__ == "__main__":
    """
    Main for test the Server
    """
    theServeur = Server("localhost", 12345, None)
    theServeur.start()
    